package com.robam.gulimall.product.service.impl;

import com.robam.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.robam.gulimall.product.entity.AttrEntity;
import com.robam.gulimall.product.service.AttrAttrgroupRelationService;
import com.robam.gulimall.product.service.AttrService;
import com.robam.gulimall.product.service.CategoryService;
import com.robam.gulimall.product.vo.AttrGroupAndAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.Query;

import com.robam.gulimall.product.dao.AttrGroupDao;
import com.robam.gulimall.product.entity.AttrGroupEntity;
import com.robam.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params, QueryWrapper<AttrGroupEntity> wrapper) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getAttrGroupInfo(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = this.baseMapper.selectById(attrGroupId);
        Long[] categoryPath = categoryService.getCategoryPath(attrGroupEntity.getCatelogId());
        attrGroupEntity.setCategoryPath(categoryPath);
        return attrGroupEntity;
    }

    @Override
    public List<AttrEntity> getAttrGroupAttrRelationByGroupId(Long groupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_group_id", groupId);
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(wrapper);
        List<Long> attrIds = relationEntities.stream().map(item -> item.getAttrId()).collect(Collectors.toList());
        List<AttrEntity> attrEntities = null;
        if(attrIds != null && attrIds.size()>0) {
            attrEntities = attrService.listByIds(attrIds);
        }
        return attrEntities;
    }

    @Override
    public PageUtils getAttrGroupAttrNoRelation(Map<String, Object> params, Long groupId) {
        // 1.只能显示和分组的三级分类相同的属性，并且属性未被关联到任何分组中
        AttrGroupEntity attrGroup = this.baseMapper.selectById(groupId);
        Long catelogId = attrGroup.getCatelogId();
        PageUtils attrGroupAttrNoRelation = attrAttrgroupRelationService.getAttrGroupAttrNoRelation(params, catelogId);
        return attrGroupAttrNoRelation;
    }

    @Override
    public void addAttrGroupAttrRelation(List<AttrAttrgroupRelationEntity> relationEntities) {
        attrAttrgroupRelationService.saveBatch(relationEntities);
    }

    @Override
    public List<AttrGroupAndAttrsVo> getAttrGroupAndAttrByCatId(Long catId) {
        // 1.查出三级分类下的分组信息
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catId);
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(wrapper);
        List<AttrGroupAndAttrsVo> attrGroupAndAttrsVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupAndAttrsVo attrGroupAndAttrsVo = new AttrGroupAndAttrsVo();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupAndAttrsVo);
            // 2.查询分组下关联的属性
            List<AttrEntity> attrEntities = this.getAttrGroupAttrRelationByGroupId(attrGroupAndAttrsVo.getAttrGroupId());
            attrGroupAndAttrsVo.setAttrs(attrEntities);
            return attrGroupAndAttrsVo;
        }).collect(Collectors.toList());
        return attrGroupAndAttrsVos;
    }

}